package com.accloud.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

public class ACObject implements Serializable {
    private static final long serialVersionUID = 16777216;
    private HashMap<String, Object> data = new HashMap<>();

    private boolean checkType(Object o) {
        return o == null || (o instanceof String) || (o instanceof Integer) || (o instanceof Long) || (o instanceof Float) || (o instanceof Double) || (o instanceof Boolean) || (o instanceof List) || (o instanceof byte[]) || (o instanceof ACObject);
    }

    private void ensureType(Object o) {
        if (!checkType(o)) {
            throw new IllegalArgumentException("unsupported type");
        }
    }

    public <T> T get(String key) {
        return (T) this.data.get(key);
    }

    public int getInt(String key) {
        Object o = this.data.get(key);
        if (o instanceof Integer) {
            return ((Integer) o).intValue();
        }
        if (o instanceof Number) {
            return ((Number) o).intValue();
        }
        if (o instanceof String) {
            try {
                return Integer.valueOf((String) o).intValue();
            } catch (NumberFormatException e) {
            }
        }
        return 0;
    }

    public long getLong(String key) {
        Object o = this.data.get(key);
        if (o instanceof Long) {
            return ((Long) o).longValue();
        }
        if (o instanceof Number) {
            return ((Number) o).longValue();
        }
        if (o instanceof String) {
            try {
                return Long.valueOf((String) o).longValue();
            } catch (NumberFormatException e) {
            }
        }
        return 0;
    }

    public float getFloat(String key) {
        Object o = this.data.get(key);
        if (o instanceof Float) {
            return ((Float) o).floatValue();
        }
        if (o instanceof Number) {
            return ((Number) o).floatValue();
        }
        if (o instanceof String) {
            try {
                return Float.valueOf((String) o).floatValue();
            } catch (NumberFormatException e) {
            }
        }
        return 0.0f;
    }

    public double getDouble(String key) {
        Object o = this.data.get(key);
        if (o instanceof Double) {
            return ((Double) o).doubleValue();
        }
        if (o instanceof Number) {
            return ((Number) o).doubleValue();
        }
        if (o instanceof String) {
            try {
                return Double.valueOf((String) o).doubleValue();
            } catch (NumberFormatException e) {
            }
        }
        return 0.0d;
    }

    public boolean getBoolean(String key) {
        Object o = this.data.get(key);
        if (o instanceof Boolean) {
            return ((Boolean) o).booleanValue();
        }
        if (!(o instanceof String)) {
            return false;
        }
        String stringValue = (String) o;
        if ("true".equalsIgnoreCase(stringValue)) {
            return true;
        }
        if ("false".equalsIgnoreCase(stringValue)) {
        }
        return false;
    }

    public String getString(String key) {
        Object o = this.data.get(key);
        if (o instanceof String) {
            return (String) o;
        }
        if (o != null) {
            return String.valueOf(o);
        }
        return "";
    }

    public List<ACObject> getList(String key) {
        Object o = this.data.get(key);
        if (o instanceof List) {
            return (List) o;
        }
        return new ArrayList();
    }

    public ACObject getACObject(String key) {
        Object o = this.data.get(key);
        if (o instanceof ACObject) {
            return (ACObject) o;
        }
        return new ACObject();
    }

    public <T> ACObject put(String name, T value) {
        ensureType(value);
        this.data.put(name, value);
        return this;
    }

    public boolean contains(String name) {
        return this.data.containsKey(name);
    }

    public Set<String> getKeys() {
        return this.data.keySet();
    }

    public ACObject add(String key, Object value) {
        List<Object> as;
        ensureType(value);
        Object o = this.data.get(key);
        if (o == null) {
            as = new ArrayList<>();
        } else if (!(o instanceof ArrayList)) {
            throw new IllegalArgumentException("this key is not an ArrayList");
        } else {
            as = (List) o;
        }
        as.add(value);
        this.data.put(key, as);
        return this;
    }

    public void delete(String key) {
        if (contains(key)) {
            this.data.remove(key);
        }
    }

    public boolean hasObjectData() {
        return !this.data.isEmpty();
    }

    public HashMap<String, Object> getObjectData() {
        return this.data;
    }

    public void setObjectData(HashMap<String, Object> data2) {
        this.data = data2;
    }

    @Override // java.lang.Object
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        ACObject object = (ACObject) o;
        if (object.getKeys().size() != getKeys().size()) {
            return false;
        }
        for (String key : object.getKeys()) {
            if (!get(key).equals(object.get(key))) {
                return false;
            }
        }
        return true;
    }

    @Override // java.lang.Object
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("{");
        Set<String> keys = getKeys();
        int i = 0;
        for (String key : keys) {
            sb.append("\"" + key + "\":");
            if (get(key) == null) {
                sb.append("\"\"");
            } else if (get(key) instanceof byte[]) {
                sb.append(Arrays.toString((byte[]) get(key)));
            } else if (get(key) instanceof List) {
                sb.append("[");
                List<Object> objects = (List) get(key);
                int j = 0;
                for (Object object : objects) {
                    if (object == null) {
                        sb.append("\"\"");
                    } else if (object instanceof String) {
                        sb.append("\"" + object.toString() + "\"");
                    } else if (checkType(object)) {
                        sb.append(object.toString());
                    }
                    j++;
                    if (j != objects.size()) {
                        sb.append(",");
                    }
                }
                sb.append("]");
            } else if (get(key) instanceof String) {
                sb.append("\"" + get(key).toString() + "\"");
            } else if (checkType(get(key))) {
                sb.append(get(key).toString());
            }
            i++;
            if (i != keys.size()) {
                sb.append(",");
            }
        }
        sb.append("}");
        return sb.toString();
    }
}
